package com.zzyy.study.config;

import com.zzyy.study.service.AiAssistant;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.qdrant.QdrantEmbeddingStore;
import io.qdrant.client.QdrantClient;
import io.qdrant.client.QdrantGrpcClient;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @auther zzyy
 * @create 2025-03-03 12:32
 */
@Configuration(proxyBeanMethods = false)
public class EmbeddingModelConfig
{
    @Bean
    public EmbeddingModel embeddingModel() {
        return OpenAiEmbeddingModel.builder()
                .apiKey(System.getenv("LANGCHAIN4J_KEY"))
                .modelName("text-embedding-v3")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();
    }

    @Bean
    public QdrantClient qdrantClient() {
        QdrantGrpcClient.Builder grpcClientBuilder =
                QdrantGrpcClient.newBuilder("127.0.0.1", 6334, false);
        return new QdrantClient(grpcClientBuilder.build());
    }

    @Bean
    public EmbeddingStore<TextSegment> embeddingStore() {
        return QdrantEmbeddingStore.builder()
                .host("127.0.0.1")
                .port(6334)
                .collectionName("test-qdrant")
                .build();
    }

    /*@Bean
    public ChatLanguageModel chatLanguageModel()
    {
        return
                OpenAiChatModel.builder()
                        .apiKey(System.getenv("LANGCHAIN4J_KEY"))
                        .modelName("qwen-turbo-0624")
                        .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                        .build();
    }

    @Bean
    public AiAssistant getAiAssistant(EmbeddingStore<TextSegment> embeddingStore,ChatLanguageModel chatLanguageModel)
    {
        return AiServices.builder(AiAssistant.class)
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(100))
                .contentRetriever(EmbeddingStoreContentRetriever.from(embeddingStore))
                .chatLanguageModel(chatLanguageModel)
                .build();
    }*/

}
